<template>
    <div>
        <Form :label-width="100" inline>
            <Form-item label="发放年月">
                <DatePicker type="month" v-model="queryParam.ssfc0002" style="width: 150px"
                            format="yyyy-MM"  :editable="false"></DatePicker>
            </Form-item>
            <Form-item label="救助对象类别">
                <Select v-model="queryParam.ssfc0004" style="width: 200px" clearable>
                    <Option value="3010">临时救助对象</Option>
                    <Option value="3040">两节救助对象</Option>
                    <Option value="3050">知青救助对象</Option>
                    <Option value="3070">电价补贴对象</Option>
                    <Option value="3080">价格补贴对象</Option>
                </Select>
            </Form-item>
            <Row class="button_row">
                 <span style="float: left;">
                     <Button type="primary" @click="handleAdd()">录入</Button>
                     <Button type="primary" class="button_item_left" @click="handleEdit()">编辑</Button>
                     <Button type="primary" class="button_item_left" @click="handleDeleteSs18900()">删除</Button>
                     <Button type="primary" class="button_item_left" @click="handleDetail()">查看</Button>
                     <Button type="primary" class="button_item_left" @click="generateRoster()">花名册生成</Button>
                     <Button type="primary" class="button_item_left" @click="rosterConfirm()">花名册确认</Button>
                 </span>
                <span style="float: right;">
                    <Button type="primary" @click="searchQuery18900">查询</Button>
                    <Button type="primary" class="button_item_left" @click="searchReset">重置</Button>
                </span>
            </Row>
        </Form>
        <Modal v-model="visible" :title="title" width="600" style="position:relative;">
            <Spin v-if="LoadingModal" size="large" fix></Spin>
            <Form ref="formData" :model="formData" :label-width="150" :rules="rules" :disabled="disableSubmit">
                <FormItem label="发放年月" prop="ssfc0002">
                    <DatePicker type="month" v-model="formData.ssfc0002" style="width: 200px"
                                @on-change="ssfc0002Change"  :editable="false"></DatePicker>
                </FormItem>
                <FormItem label="救助对象类别" prop="ssfc0004">
                    <Select v-model="formData.ssfc0004" style="width: 200px" clearable>
                        <Option value="3010">临时救助对象</Option>
                        <Option value="3040">两节救助对象</Option>
                        <Option value="3050">知青救助对象</Option>
                        <Option value="3070">电价补贴对象</Option>
                        <Option value="3080">价格补贴对象</Option>
                    </Select>
                </FormItem>
                <FormItem label="备注">
                    <Input v-model="formData.remark" maxlength="100" show-word-limit type="textarea" placeholder="请输入备注"
                           style="width: 200px"/>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="text" size="large" @click="visible = false">取消</Button>
                <template v-if="!disableSubmit">
                    <Button type="primary" size="large" @click="handleSubmit('formData')">确定</Button>
                </template>
            </div>
        </Modal>
        <div style="margin-top: 20px">
            <vxe-table border
                       resizable
                       row-id="id"
                       :row-key="true"
                       show-overflow="tooltip"
                       align="center"
                       header-align="center"
                       ref="xTree"
                       :loading="Loading"
                       :radio-config="{highlight: true}"
                       @cell-click="selectChange"
                       :data="tableData">
                <vxe-table-column type="radio" width="60" align="center" fixed="left"/>
                <vxe-table-column field="ssfc0002" title="发放年月" align="center" width="150" fixed="left"
                                  :formatter="Time1"/>
                <vxe-table-column field="azaa0002" title="区县名称" align="center" width="150" fixed="left"/>
                <vxe-table-column field="ssfc0004_name" title="救助对象类别" align="center" width="230" fixed="left"/>
                <vxe-table-column field="ssfc0005_name" title="是否生成" align="center" width="130"/>
                <vxe-table-column field="ssfc0006" title="生成时间" align="center" width="150" :formatter="renderTime"/>
                <vxe-table-column field="ssfc0007" title="生成人姓名" align="center" width="170"/>
                <vxe-table-column field="ssfc0008_name" title="生成是否成功" align="center" width="150"/>
                <vxe-table-column field="ssfc0010_name" title="是否确认" align="center" width="130"/>
                <vxe-table-column field="ssfc0012" title="确认时间" align="center" width="150" :formatter="renderTime"/>
                <vxe-table-column field="ssfc0011" title="确认人" align="center" width="150"/>
            </vxe-table>

            <Page :total="dataCount" :page-size="pageSize" show-total class="paging"
                  @on-change="handleTableChange" :current="pageNo"></Page>
        </div>
    </div>
</template>

<script>
    import {postAction} from '@/api/request'
    import {CommonMixin} from '@/views/common/js/CommonMixin'
    import DateFormat from '@/plugins/format.js'

    export default {
        mixins: [CommonMixin],
        components: {},
        data() {
            return {
                errorBtn: false,
                isErrorInfo: false,
                formErorrData: {},
                LoadingModal: false,
                // 录入花名册时发放时间不能选择过去
                disabledMonth: {
                    disabledDate(date) {
                        let nowTime = new Date()
                        let month = nowTime.getMonth() + 1
                        let year = nowTime.getFullYear()
                        if (month - 1 == 0) {
                            year = year - 1;
                            month = 12;
                        }
                        return date && date.valueOf() < new Date(`${year}-${month}`);
                    }
                },
                noBankInfo: false,
                validateInfo: false,
                validateInfoData: [],
                noBankInfoData: [],
                rules: {
                    ssfc0002: [
                        {required: true, message: '请选择"发放年月"', trigger: 'blur,change', type: 'date'}
                    ],
                    ssfc0004: [
                        {required: true, message: '请选择"救助对象类别"', trigger: 'blur'}
                    ]
                },
                ss18900Id: '',
                tableData: [],
                Loading: false,
                url: {
                    list: "/api/ac/subsistence/ss18900ZXService/getPageList",
                    delete: "/api/ac/subsistence/ss18900ZXService/deleteData",
                    generate: "/api/ac/subsistence/ss18900ZXService/createRosterData",
                    confirm: "/api/ac/subsistence/ss18900ZXService/updateAndDoRosterConfirm",
                    exportExcel: "/api/ac/subsistence/ss00005Service/exportErrorExcel",
                },
                formData: {},
                queryParam: {},
                fieldsToValidate: ["ssfc0002", "ssfc0004"],
                fieldsToErrorValidate: ["pshr0006", "pshr0007"],
                results: {},
                visible: false,
                different: "",
                dataCount: 0,
                dataCountNoBankInfo: 0,
                dataCountNoValidateInfo: 0,
                pageSize: 10,
                modal: false,
                title: "",
                disableSubmit: false,
                // dataNum: 0,
                selectedData: '',
                visibleForCheck: false,
                ssfc0014: '',
                rowData: [],
                selectRow: null,
            }
        },
        methods: {
            editError(row) {
                this.formErorrData.id = row.id;
                this.visibleError = true;
            },
            exportErrorExcel() {
                this.queryParam.ss18900Id = this.ss18900Id;
                this.exportExcel();
                this.queryParam.ss18900Id = '';
            },

            selectChange({row}) {
                this.$refs.xTree.setRadioRow(row);
                this.selectedData = row;
            },

            generateRoster() {
                let that = this
                if (that.selectedData == '') {
                    that.$Message.error('请选择要生成救助花名册的数据！')
                    return false
                }
                let row = that.selectedData;
                let url = that.url.generate;
                let msg = '确定生成此数据的救助对象花名册吗？';
                let ssfc0005 = row.ssfc0005;
                if ("1" == ssfc0005) {
                    msg = '已经生成此数据的救助对象花名册，确定重新生成吗？';
                }
                if (row.ssfc0010 == '1') {
                    // this.$Message.error("已经确认救助花名册，不允许再次查验！");
                    this.$Message.error("已经确认救助花名册，不允许再次生成！");
                    return;
                }
                let dataParam = {};
                dataParam.id = row.id;
                that.$Modal.confirm({
                    title: msg,
                    onOk: () => {
                        postAction(url, dataParam).then((res) => {
                            if (res.data.errcode != 0) {
                                this.$Message.error(res.data.errmsg);
                            } else {
                                that.$Message.success(res.data.data.msg);
                                that.loadData();
                            }
                        }).finally(() => {
                            that.Loading = false
                        })
                    },
                    onCancel: () => {
                    }
                });
                // }

            },
            rowSelect(row, index) {
                this.selectedData = row;
            },
            handleAdd() {
                this.$refs['formData'].resetFields();
                let that = this;
                that.title = "花名册录入";
                that.disableSubmit = false;
                let record = {};
                that.visible = true;
                that.different = that.uuid();
                that.formData = Object.assign({}, record)
                that.formData.ssfc0002 = new Date();
            },
            handleDetail() {
                let that = this;
                if (this.selectedData == '') {
                    this.$Message.error('请选中一条数据!')
                    return false;
                }
                let record = that.selectedData;
                that.title = "详情";
                that.disableSubmit = true;
                if (record.id) {
                    that.visible = true;
                    that.different = that.uuid();
                    that.formData = Object.assign({}, record);
                    that.formData.ssfc0002 = DateFormat(that.formData.ssfc0002, 'YYYY-MM')
                } else {
                    that.visible = false
                }
            },
            handleEdit(index) {
                let that = this;
                this.$refs['formData'].resetFields();
                if (this.selectedData == '') {
                    this.$Message.error('请选中一条数据!')
                    return false;
                } else if (that.selectedData.ssfc0005 == '1') {
                    that.$Message.error("已经生成救助花名册，不允许编辑！");
                    return;
                } else if (that.selectedData.ssfc0010 == '1') {
                    that.$Message.error("已经确认救助花名册，不允许编辑！");
                    return;
                } else {
                    let record = that.selectedData;
                    that.title = "花名册编辑";
                    that.disableSubmit = false;
                    if (record.id) {
                        that.visible = true;
                        that.different = that.uuid();
                        that.formData = Object.assign({}, record);
                        that.formData.ssfc0002 = DateFormat(that.formData.ssfc0002, 'YYYY-MM');
                    } else {
                        that.visible = false
                    }
                }
            },
            handleDeleteSs18900() {
                let that = this;
                if (this.selectedData == '') {
                    this.$Message.error('请选中一条数据!')
                    return false;
                } else if (that.selectedData.ssfc0005 == '1') {
                    that.$Message.error("已经生成救助花名册，不允许删除！");
                    return;
                } else if (that.selectedData.ssfc0010 == '1') {
                    that.$Message.error("已经确认救助花名册，不允许删除！");
                    return;
                } else {
                    this.$Modal.confirm({
                        title: '确定删除吗？',
                        onOk: () => {
                            if (!this.url.delete) {
                                this.$Message.error("请设置url.delete属性!")
                                return
                            }
                            postAction(that.url.delete, that.selectedData).then((res) => {
                                if (res.data.errcode == 0) {
                                    //重新计算分页问题
                                    that.reCalculatePage(1);
                                    that.$Message.success("删除成功！");
                                } else {
                                    that.$Message.error(res.data.errmsg);
                                }
                            });
                        }
                    })
                }
            },
            ssfc0002Change() {
                this.formData.ssfc0002 = this.formData.ssfc0002;
            },
            handleSubmit(name) {
                let that = this;
                let validate = true;
                let err_msg = [];
                that.LoadingModal = true;
                for (let item of that.fieldsToValidate) {
                    that.$refs[name].validateField(item, (error) => {
                        if (error) {
                            validate = false;
                            err_msg.push(error);
                        }
                    })
                }
                if (validate) {
                    let dataParam = {
                        id: that.formData.id,
                        ssfc0002: DateFormat(that.formData.ssfc0002, 'YYYY-MM-DD'),
                        ssfc0004: that.formData.ssfc0004,
                        remark: that.formData.remark
                    }
                    that.Loading = true;
                    let url = "/api/ac/subsistence/ss18900ZXService/createData";
                    if (this.formData.id) {
                        url = "/api/ac/subsistence/ss18900ZXService/updateData";
                    }
                    postAction(url, dataParam).then((res) => {
                        if (res.data.errcode != 0) {
                            this.$Message.error(res.data.errmsg);
                        } else {
                            this.modalFormOk();
                            this.visible = false;
                            this.$Message.info(res.data.data.Message);
                        }
                    }).finally(() => {
                        that.Loading = false;
                        that.LoadingModal = false;
                    });
                } else {
                    that.$Message.error(err_msg[0]);
                    that.LoadingModal = false;
                    return;
                }
            },
            rosterConfirm() {
                let that = this;
                if (this.selectedData == '') {
                    this.$Message.error('请选中一条数据!')
                    return false;
                } else if (that.selectedData.ssfc0005 != '1') {
                    this.$Message.error("未生成救助花名册，不允许进行花名册确认！");
                    return;
                } else if (that.selectedData.ssfc0010 == '1') {
                    this.$Message.error("已经确认救助花名册，不允许重复确认！");
                    return;
                } else {
                    this.$Modal.confirm({
                        title: '确认此数据的专项对象花名册后，不允许重复生成本月数据！确定当前操作吗？',
                        onOk: () => {
                            let record = that.selectedData;
                            record.ssfc0002 = DateFormat(record.ssfc0002, 'YYYY-MM');
                            postAction(this.url.confirm, record).then((res) => {
                                if (res.data.errcode != 0) {
                                    that.$Message.error(res.data.data.errmsg);
                                } else {
                                    if (res.data.data.success=='0'){
                                        that.$Message.error(res.data.data.msg);
                                    }else if(res.data.data.success=='1'){
                                        that.$Message.success(res.data.data.msg);
                                    }
                                    that.loadData();
                                }
                            }).finally(() => {
                                that.Loading = false;
                            });
                        }
                    })
                }
            },
            searchYearMonth: function ($event) {
                this.formData.ssfc0002 = $event;
                eventBus.$emit('ssfc0002', this.formData.ssfc0002);
                tableOperationReport.methods.showAll();
            },
            cancel() {
                visibleForCheck = false;
                // this.dataNum = null;
                this.selectedData = null;
            },
            searchQuery18900() {
                let that = this;
                that.queryParam.ssfc0002 = DateFormat(that.queryParam.ssfc0002, 'YYYY-MM-DD');
                that.selectedData = '';
                this.loadData();
            },
            searchReset() {
                this.queryParam = {}
                this.selectedData = '';
                this.loadData();
            },
        },
        created() {
            this.loadData();
        },
    }
</script>
<style scoped>
    /deep/ .ivu-form-item-error-tip {
        display: none;
    }

    .itemStyle {
        width: 180px
    }

    .head-img1 {
        height: 200px;
        width: 250px;
        margin-top: 15px;
    }

    .head-img {
        height: 200px;
        width: 250px;
    }

    .imgBackColor {
        background-color: WhiteSmoke;
        height: 250px;
    }

    .positiveTDiv {
        height: 240px;
        width: 75px;
        line-height: 240px;
        background-color: #7CCD7C;
        margin-bottom: 10px;
        color: white;
        font-size: 14px;
        font-weight: 700;
    }

    /deep/ .ivu-table .demo-table-info-cell-age {
        background-color: #ff6600;
        color: #fff;
    }

    /deep/ .ivu-table .demo-table-info-cell-ssfc0015_name {
        background-color: #f33e3e;
        color: #fff;
    }
</style>
